package com.leonzhangxf.ioc.container.extension;

import com.leonzhangxf.ioc.bean.HelloWorldService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * A use demo for {@link BeanFactoryPostProcessor}.
 *
 * @author leonzhangxf
 */
@Component
public class HelloWorldBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        String[] beanNamesForType = beanFactory.getBeanNamesForType(HelloWorldService.class);
        if (beanNamesForType.length > 0) {
            for (String beanNames : beanNamesForType) {
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanNames);
                System.out.println(String.format("%s. Get the bean impl class name %s.",
                    HelloWorldBeanFactoryPostProcessor.class.getSimpleName(),
                    beanDefinition.getBeanClassName()));
            }
        }
    }

    @Override
    public int getOrder() {
        return 100;
    }
}
